<?php

/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 **/

class temoignageSrv {

    /**
     *Fonction pour afficher la liste des témoignages
    */
    public function getAll($_iOffset, $_iParPage){

        $temoignage= jDao::get("commun~ao_temoignage");

        $condition  = jDao::createConditions();
        $condition->addItemOrder('date_ajout', 'DESC');
        $tResult['temoignage']     =  $temoignage->findBy($condition,$_iOffset,$_iParPage)->fetchAll();
        $tResult['nbRecords']      =  $temoignage->countBy($condition);

        return $tResult;
    }

    /**
     * Liste des temoignages
     */
    public function liste($limit = 0){

        $temoignage= jDao::get("commun~ao_temoignage");

        $condition  = jDao::createConditions();
        $condition->addCondition('statut','=', STATUT_ON);
        $condition->addItemOrder('date_ajout','desc');
        if ($limit ) 
            return $temoignage->findBy($condition, 0, $limit)->fetchAll();
        return $temoignage->findBy($condition)->fetchAll();
    }

    /**
     * Fonction pour ajouter un témoignage
     * @param object $oTemoignage
    */
    public function ajouter($oTemoignage){

        jClasses::inc("commun~communSrv");
        
        $dao = jDao::get('commun~ao_temoignage');
        $temoignage= jDao::createRecord("commun~ao_temoignage");

        $vendeur = explode( '_',$oTemoignage->temoignage_vendeur);

        $temoignage->vendeur_id                   = $vendeur[0];
        $temoignage->vendeur_nom                  = $vendeur[1];
        $temoignage->temoignage_titre             = $oTemoignage->temoignage_titre;
        $temoignage->temoignage_description       = $oTemoignage->temoignage_content;
        $temoignage->location                     = $oTemoignage->temoignage_departement;
        $temoignage->statut                       = $oTemoignage->temoignage_status;
        $temoignage->date_validation              = communSrv::toDateSql($oTemoignage->temoignage_datevalide);
        $temoignage->date_refus                   = communSrv::toDateSql($oTemoignage->temoignage_daterefus);
        $temoignage->date_suppr                   = communSrv::toDateSql($oTemoignage->temoignage_datesuppr);

        if ($dao->insert($temoignage)) {
            return $temoignage ;
        } else {
            return false ;
        }
    }

    /**
     * Fonction pour ajouter un témoignage depuis la gestion d'annonce en front, apres suppression d'une annonce
     * @param object $temoignage
    */
    public function add($temoignage){

        $dao = jDao::get('commun~ao_temoignage');
        $temoignageItem = jDao::createRecord("commun~ao_temoignage");

        $temoignageItem->vendeur_id              = $temoignage->vendeur_id;
        $temoignageItem->vendeur_nom             = $temoignage->vendeur_nom;
        $temoignageItem->temoignage_titre        = $temoignage->temoignage_titre;
        $temoignageItem->temoignage_description  = $temoignage->temoignage_description;
        $temoignageItem->location                = $temoignage->location;
        $temoignageItem->date_ajout              = $temoignage->date_ajout;
        $temoignageItem->statut                  = $temoignage->statut;
        $temoignageItem->date_validation         = $temoignage->date_validation;
        $temoignageItem->date_refus              = $temoignage->date_refus;
        $temoignageItem->date_suppr              = $temoignage->date_suppr;

        if ($dao->insert($temoignageItem)) {
            return $temoignageItem ;
        } else {
            return false ;
        }
    }

    /**
     * Fonction pour modifier un témoignage
     * @param object $oTemoignage
     */
      public function modifier($oTemoignage){

        jClasses::inc("commun~communSrv");

        $dao = jDao::get('commun~ao_temoignage');
        $temoignage= $dao->get($oTemoignage->temoignage_id);

        $vendeur = explode( '_',$oTemoignage->temoignage_vendeur);

        $temoignage->vendeur_id                   = $vendeur[0];
        $temoignage->vendeur_nom                  = $vendeur[1];
        $temoignage->temoignage_titre             = $oTemoignage->temoignage_titre;
        $temoignage->temoignage_description       = $oTemoignage->temoignage_content;
        $temoignage->location                     = $oTemoignage->temoignage_departement;
        $temoignage->statut                       = $oTemoignage->temoignage_status;
        $temoignage->date_validation              = communSrv::toDateSql($oTemoignage->temoignage_datevalide);
        $temoignage->date_refus                   = communSrv::toDateSql($oTemoignage->temoignage_daterefus);
        $temoignage->date_suppr                   = communSrv::toDateSql($oTemoignage->temoignage_datesuppr);

        if ($dao->update($temoignage)) {
            return $temoignage ;
        } else {
            return false ;
        }

     }
 
     /**
     * Fonction permettant d'avoir un témoignage à partir de son id
     * @param object $_id
     */
    public function chargeParId($_id){

        $dao = jDao::get('commun~ao_temoignage');

        return  $dao->get($_id);
    }

    /**
     * Fonction permettant d'initialiser un objet temoigange
     * @return object
     *
     */
    public function init () {

        return jDao::createRecord("commun~ao_temoignage");
    }
    
    /**
     * Liste des temoignages
     */
    public static function listeTemoignagesVendeur($_iDebutListe = 0,$_iParPage = 4){

        $cnx = jDb::getConnection();
        $sql  = " SELECT ATem.temoignage_id ,ATem.temoignage_titre,ATem.temoignage_description,ATem.vendeur_id,ATem.vendeur_nom,AVO.IdOccasion,AVO.Occas_Fimage,AMa.marque_logo,AMa.marque_libelle,AMo.modele_libelle";
        $sql .= " from ao_temoignage ATem";
        $sql .= " INNER JOIN ao_voiture_occasion AVO ON AVO.Occas_Vendeur = ATem.vendeur_id";
        $sql .= " LEFT JOIN ao_marque AMa ON AMa.marque_id = AVO.Occas_Marque";
        $sql .= " LEFT JOIN ao_modele AMo ON AMo.modele_id = AVO.Occas_Modele";
        $sql .= " ORDER BY ATem.date_ajout DESC";
        $rs = $cnx->query($sql);
        
        $tResult['temoignage']  = $rs->fetchAll();
        $iSize                  = sizeof($tResult['temoignage']);
        $tResult['temoignage']     = array_slice($tResult['temoignage'], $_iDebutListe, $_iParPage);
        $tResult['nbRecords']   = $iSize;
        return $tResult;
    }

    /**
     * Id de l'annnonce pour un temoignage
     * @param int $idTemoignage
     */
    public static function getIdAnnonce($idTemoignage){

        $cnx = jDb::getConnection();
        $sql  = " SELECT Occas_NumImmatricul, IdOccasion";
        $sql .= " FROM ao_voiture_occasion AVO";
        $sql .= " INNER JOIN ao_temoignage ATem ON AVO.Occas_Vendeur = ATem.vendeur_id";
        $sql .= " WHERE temoignage_id = " . $idTemoignage;
        $sql .= " LIMIT 1";
        $rs = $cnx->query($sql);
        
        return  $rs->fetchAll();
    }

    /**
     * Liste des temoignages
     * @param array $idVendeurListe
     */
    public static function getIdOccasionByVendeur($idVendeurListe){

        $cnx = jDb::getConnection();
        $sql  = " SELECT IdOccasion, Occas_Vendeur";
        $sql .= " FROM ao_voiture_occasion";
        $sql .= " WHERE Occas_Vendeur IN (".implode(',', $idVendeurListe).")";
        $sql .= " LIMIT " . sizeof($idVendeurListe);

        $rs   = $cnx->query($sql);

        $result = array();
        while($row = $rs->fetch()){
            if ($row->Occas_Vendeur && $row->IdOccasion) 
            $result[$row->Occas_Vendeur] = $row->IdOccasion;
        }

		return $result;
    }
    
    /*
     * fonction pour recuperer photo annonce
     */
    public static function getAnnonceThumb($tem){

        jClasses::inc('annonce~annonceSrv');
        jClasses::inc('commun~toolsFile');
        
        $annonceThumb = array();
        foreach ($tem as $key => $value) {
            $trouve = false;
            if($value->Occas_Fimage <>''){
                $photoOccasion = $value->Occas_Fimage;
                $photo =  array();
                $imgPath = PHOTO_ANNONCE_PATH;
                //tester si le repertoire existe
                $dirPath = JELIX_APP_WWW_PATH . PATH_ANNONCE_IMG . $photoOccasion;
                if (is_dir($dirPath)){
                    //lister les images dans le repertoire
                    if ($dh = opendir($dirPath)) {
                        while (($file = readdir($dh)) !== false) {
                            $filename = $dirPath . '/'. $file;
                            $ext = explode(".", $file);
                            if (file_exists($filename) && isset($ext[1]) && toolsFile::is_image($ext[1])) {
                                // formation de l'objet photo
                                $images = new stdClass();
                                $images->photo_name = PATH_ANNONCE_IMG . $photoOccasion . '/' . $file;
                                $photo[] = $images;
                                $imgPath = "";
                                $trouve = true;
                            }
                        }
                        closedir($dh);
                    }
                }

            }
            if($trouve) {
                array_push($annonceThumb,$photo[0]->photo_name);
            } else {
                $photoSrc   = annonceSrv::getPhotoOccasion($value->IdOccasion);
                if(sizeof($photoSrc) > 0)
                    array_push($annonceThumb, PHOTO_ANNONCE_PATH . $photoSrc[0]->photo_name);
                elseif($value->marque_logo != ''){
                    array_push($annonceThumb,$value->marque_logo);
                } else {
                    array_push($annonceThumb,PATH_DESIGN_FRONT_IMAGES . 'no_image.jpg');
                }
                    
            } 
        }
        
        
        return $annonceThumb;
    }



}
